<?php
/**
 * Created by PhpStorm.
 * User: wx:wenyi2048
 * Date: 2022/3/20 0020
 * Time: 14:28
 */

namespace app\common\service;


use think\Db;
use think\Exception;

class TypeService extends Service
{
    protected $tableName = 'sms_mk_types';

    /**
     * @param $view
     * @param $params
     * @param int $isdelete
     */
    public function searchPage(&$view,$params,$isdelete = 0){
        $this->db->alias('t')
            ->where('t.level','=',2)
            ->join('sms_mk_types p','t.pid = p.id','left')
            ->field('t.*')
            ->where('t.isdelete','=',0)
            ->where('p.isdelete','=',0);
        if(@$params['name']) $this->db->where('name','like','%'.$params['name'].'%');
        $this->pageList($view,$params);
    }

    public function treeList($isdelete = 0){

        $this->db->where('isdelete','=',$isdelete)
            ->order('sort','asc')
            ->field('*');
        $dataList = $this->db->select();
        $result = list_to_tree($dataList,'id','pid','_child');
        return $result;
    }

    public function getTypes($ids){
        if(!is_array($ids)) $ids = array($ids);
        return $this->db->whereIn('id',$ids)->select();
    }

    public function getNodeSearch($leTypeId,$name){
        $this->db->where('pid','=',$leTypeId);
        if($name) $this->db->where('name','like','%'.$name.'%');
        return  $this->db->select();
    }

    public function topList(&$view,$params,$isdelete = 0){
        $this->db->alias('type')
            ->join('sms_mk_types_tops t','type.id = t.type_id')

            ->join('sms_mk_types p','type.pid = p.id','left')
            ->where('p.isdelete','=',$isdelete)

            ->where('type.isdelete','=',$isdelete)
            ->order('type.id','desc')
            ->field('type.*');
        if(@$params['name']) $this->db->where('type.name','like','%'.$params['name'].'%');

        $this->pageList($view,$params);
    }

    public function datalistTop()
    {
        $this->db->alias('type')
            ->join('sms_mk_types_tops t','t.type_id = type.id','right')
            ->join('sms_mk_types p','type.pid = p.id','left')
            ->where('p.isdelete','=',0)
            ->where('type.isdelete','=',0)
            ->field('type.*');
        return parent::datalist(); // TODO: Change the autogenerated stub
    }

    public function topAdd($typeIds){
        if(!$typeIds) throw new Exception('请选择分类');
        if(!is_array($typeIds)) $typeIds = array($typeIds);
        if($typeIds)
            Db::table('sms_mk_types_tops')->whereIn('type_id',$typeIds)->delete();
        $ids = [];
        foreach ($typeIds as $id){
            $ids[]['type_id'] = $id;
        }
        if(!$ids)  throw new Exception('没有找到分类');
        return Db::table('sms_mk_types_tops')->insertAll($ids);
    }

    public function topDelete($typeIds){
        if(is_string($typeIds)) $typeIds = explode(',',$typeIds);
        if(!is_array($typeIds)) return 0;
        return Db::table('sms_mk_types_tops')->whereIn('type_id',$typeIds)->delete();
    }

}